package com.tencent.wxcloudrun.service;

import com.tencent.wxcloudrun.model.Product;
import com.tencent.wxcloudrun.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class ProductService {

    @Autowired
    private ProductRepository productRepository;

    /**
     * 获取所有已发布的商品
     */
    public List<Product> getAllPublishedProducts() {
        return productRepository.findByPublishedTrue();
    }

    /**
     * 通过 ID 获取商品详情
     */
    public Optional<Product> getProductById(Long id) {
        return productRepository.findById(id);
    }

    public Optional<Product> getProductByLimitAmount(Integer limitAmount) {
        return productRepository.findByPublishedTrueAndLimitAmount(limitAmount);
    }

    /**
     * 通过关键字搜索商品
     */
    public List<Product> searchProducts(String keyword) {
        return productRepository.findByNameContaining(keyword);
    }
}
